package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.dcyx.mapper.TestAnswersMapper;
import com.danciyixue.boot.dcyx.service.TestAnswersService;
import com.danciyixue.boot.dcyx.model.entity.TestAnswers;
import com.danciyixue.boot.dcyx.model.form.TestAnswersForm;
import com.danciyixue.boot.dcyx.model.query.TestAnswersQuery;
import com.danciyixue.boot.dcyx.model.vo.TestAnswersVO;
import com.danciyixue.boot.dcyx.converter.TestAnswersConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 测试答案选项服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:29
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class TestAnswersServiceImpl extends ServiceImpl<TestAnswersMapper, TestAnswers> implements TestAnswersService {

    private final TestAnswersConverter testAnswersConverter;

    /**
    * 获取测试答案选项分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<TestAnswersVO>} 测试答案选项分页列表
    */
    @Override
    public IPage<TestAnswersVO> getTestAnswersPage(TestAnswersQuery queryParams) {
        Page<TestAnswersVO> pageVO = this.baseMapper.getTestAnswersPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取测试答案选项表单数据
     *
     * @param id 测试答案选项ID
     * @return 测试答案选项表单数据
     */
    @Override
    public TestAnswersForm getTestAnswersFormData(Long id) {
        TestAnswers entity = this.getById(id);
        return testAnswersConverter.toForm(entity);
    }
    
    /**
     * 新增测试答案选项
     *
     * @param formData 测试答案选项表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveTestAnswers(TestAnswersForm formData) {
        TestAnswers entity = testAnswersConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新测试答案选项
     *
     * @param id   测试答案选项ID
     * @param formData 测试答案选项表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateTestAnswers(Long id,TestAnswersForm formData) {
        TestAnswers entity = testAnswersConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除测试答案选项
     *
     * @param ids 测试答案选项ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteTestAnswerss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的测试答案选项数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
